fix(LineChart): correct PointMode valueIndex and dot alignment#173
fix(LineChart): correct PointMode valueIndex and dot alignment#173saadkhalidkhan wants to merge 3 commits into
Conversation
Replace indexOfFirst lookup that biased toward the next index due to floating-point rounding. Find the nearest xPositions entry within the PointMode threshold and snap popups to the exact data point value.
Use the same x coordinates as the line path when drawing dots so marker positions stay consistent with large datasets and view ranges.
|
Hi @saadkhalidkhan, there is issue in this PR, imagine you have just one data and you enabled dots, the right thing is to see one dot in first of chart like current version: |
With only one value, xPositions was ending up as chart width, so the dot showed on the right. Keep it at x=0 like before.
|
Thanks for catching that. When there was only one value, Pushed a fix for that case. |
📝 WalkthroughWalkthroughChangesThe line chart now uses consistent x-position data for single-point and multi-point series. Dot placement uses those positions directly, while popup selection resolves the nearest point and handles single-value series explicitly. Line Chart Alignment
Estimated code review effort: 3 (Moderate) | ~20 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
compose-charts/src/commonMain/kotlin/ir/ehsannarmani/compose_charts/LineChart.kt (1)
651-680: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winNearest-index logic looks correct; consider unit tests.
The bounds handling and nearest-neighbor search are correct (verified the
filter { it <= lastIndex }guard protects against stale/mismatchedxPositionssizes, and monotonicxPositionsguarantees results stay within[startIndex, endIndex]). Given issue#129already reported two related index/positioning bugs in this area, and the PR itself lists dense-dot/30-point manual validation as pending, adding focused unit tests forfindNearestValueIndex/calculateValueIndex(single point, 2 points, dense clusters, threshold boundary) would cheaply guard against regressions here.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@compose-charts/src/commonMain/kotlin/ir/ehsannarmani/compose_charts/LineChart.kt` around lines 651 - 680, Add focused unit tests for findNearestValueIndex and calculateValueIndex covering single-point and two-point inputs, dense point clusters, and threshold boundary behavior. Verify nearest indices remain within the available values range, including stale or mismatched xPositions, and preserve the existing fallback behavior for values outside the threshold.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In
`@compose-charts/src/commonMain/kotlin/ir/ehsannarmani/compose_charts/LineChart.kt`:
- Around line 651-680: Add focused unit tests for findNearestValueIndex and
calculateValueIndex covering single-point and two-point inputs, dense point
clusters, and threshold boundary behavior. Verify nearest indices remain within
the available values range, including stale or mismatched xPositions, and
preserve the existing fallback behavior for values outside the threshold.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: f9ff9de9-108d-48d1-bcf2-e67064c221e1
📒 Files selected for processing (2)
compose-charts/src/commonMain/kotlin/ir/ehsannarmani/compose_charts/LineChart.ktcompose-charts/src/commonMain/kotlin/ir/ehsannarmani/compose_charts/extensions/line_chart/Line.kt


Summary
valueIndexinPopupProperties.Mode.PointModefor large datasets (reported with 30 data points)indexOfFirst { x >= xPosition }with nearest-neighbor lookup onpathData.xPositionsto avoid off-by-one errors from floating-point roundingxPositionsfor consistent marker placementFixes #129
Root cause
calculateValueIndexmapped touch position usingindexOfFirst { x >= xPosition }against path bounds width. Small floating-point differences pushed the index to the next data point (e.g. index 4 instead of 3). PointMode also reused interpolated popup values instead of the tapped point.Commits
fix(LineChart): correct PointMode valueIndex with nearest data pointfix(LineChart): align dot x positions with line path xPositionsTest plan
:compose-charts:compileDebugKotlinAndroidbuilds successfully:app:compileDebugKotlinAndroidbuilds successfullyPopupProperties.Mode.PointMode()— tap each dot,valueIndexmatches label indexMode.Normal— behavior unchangedSummary by CodeRabbit